package cn.skylark.wcm.common.util;

import cn.skylark.wcm.common.splitter.pdf.pdfdom.PDFDomTree;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.w3c.dom.Document;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PdfUtils {
    public String PdfPaging(String fileName) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateFile = sdf.format(date);
        try {

            File file = new File(fileName);
            PDDocument pdDocument = null;
            boolean sort = false;
            pdDocument = PDDocument.load(file);
            String oldName = file.getName();
            String name = oldName.substring(0, oldName.lastIndexOf("."));
            String newfilename = fileName.substring(0, fileName.lastIndexOf("."));
            Integer startPage = 1;
            Integer endPage = 100;
            PDFDomTree pdfDomTree = null;
            File newfile = new File(newfilename + "_"+dateFile);
            if (!newfile.exists()) {
                newfile.mkdirs();
            }
            for (int i = startPage; i <= pdDocument.getNumberOfPages(); i++) {
                pdfDomTree = new PDFDomTree();
                pdfDomTree.setSortByPosition(sort);
                // 设置起始页
                pdfDomTree.setStartPage(i);
                // 设置结束页
                pdfDomTree.setEndPage(i);
                Document document = pdfDomTree.createDOM(pdDocument);
                String aa = PdfUtils.doc2String(document);
                byte[] bytes;
                bytes = aa.getBytes();
                //将html写入本地文件 
                FileOutputStream fos = new FileOutputStream(newfilename + "_"+dateFile + File.separator + name + "_" + i + ".html");
                fos.write(bytes);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateFile;
    }


    public static String doc2String(Document doc) {
        try {
            Source source = new DOMSource(doc);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "html");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (Exception e) {
            return null;
        }
    }

    public void convertTxt(String fileName, String[] remotepath, String dateFile) {
        try {
            File file = new File(fileName);
            boolean sort = false;
            String outpath = fileName.substring(0, fileName.lastIndexOf(".")) + "_"+dateFile;
            String name = file.getName();
            String newName = name.substring(0, name.lastIndexOf("."));
            PDDocument pdDocument = null;
            PDFTextStripper stripper = null;
            pdDocument = PDDocument.load(file);

            Integer startPage = 1;
            Integer endPage = 100;
            for (int i = startPage; i <= pdDocument.getNumberOfPages(); i++) {
                stripper = new PDFTextStripper();
                stripper.setSortByPosition(sort);
                stripper.setStartPage(i);
                stripper.setEndPage(i);
                String newRemotepath = "";
                String filename = "";
                for (int j = 0; j < remotepath.length; j++) {
                    filename = remotepath[j].substring(remotepath[j].lastIndexOf(">") + 1);
                    if (filename.equals(newName + "_" + i)) {
                        newRemotepath = remotepath[j].substring(0, remotepath[j].lastIndexOf(">"));
                    }
                }
                String textT = newName + i + "\r\n" + Constants.SERVERPATH+"/"+"upload/html/"+ newRemotepath.substring(newRemotepath.lastIndexOf("/")+1) + "\r\n" + stripper.getText(pdDocument);
                byte[] bytes;
                bytes = textT.getBytes();
                //将txt写入本地文件 
                FileOutputStream fos = new FileOutputStream(outpath + File.separator + newName + "_" + i + ".txt");
                fos.write(bytes);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
